﻿namespace System.Web.Routing
{
    public class UrlRoutingNetModule : INetHttpModule
    {
        private static readonly object _contextKey = new object();
        private UrlRoutingModule _parent = new UrlRoutingModule();

        protected virtual void Dispose() { }

        protected virtual void Init(NetHttpApplication application)
        {
            if (application.Context.Items[_contextKey] == null)
            {
                application.Context.Items[_contextKey] = _contextKey;
                application.PostResolveRequestCache += new EventHandler(OnApplicationPostResolveRequestCache);
            }
        }

        private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
        {
            var context = ((NetHttpApplication)sender).Context;
            _parent.PostResolveRequestCache(context);
        }

        void INetHttpModule.Dispose() { Dispose(); }
        void INetHttpModule.Init(NetHttpApplication application) { Init(application); }
    }
}
